package com.example.gymmanagement.controller;


import com.example.gymmanagement.Utils.Result;
import com.example.gymmanagement.Utils.StatusCode;
import com.example.gymmanagement.domain.Rent;
import com.example.gymmanagement.service.RentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/rent")
public class RentController {
    @Autowired
    private RentService rentService;

    @PostMapping("/add")
    @ApiOperation("添加租借记录")
    public Result save(@RequestBody Rent rent,@RequestHeader("Authorization") String token){
        boolean flag = this.rentService.insert(rent);
        String msg=flag?"添加成功":"添加失败";
        return new Result(flag? StatusCode.SAVE_OK:StatusCode.SAVE_ERR,flag,msg);
    }

    @PostMapping("/delete/id")
    @ApiOperation("按id删除租借记录")
    public Result delete(@PathVariable Integer id,@RequestHeader("Authorization") String token){
        boolean flag = this.rentService.delete(id);
        String msg = flag?"删除成功":"删除失败";
        return new Result(flag?StatusCode.DELETE_OK:StatusCode.DELETE_ERR,flag,msg);
    }

    @PostMapping("/update")
    @ApiOperation("更新租借记录")
    public Result update(@RequestBody Rent rent,@RequestHeader("Authorization") String token){
        boolean flag = this.rentService.updateRent(rent);
        String msg = flag?"更新成功":"更新失败";
        return new Result(flag? StatusCode.UPDATE_OK:StatusCode.UPDATE_ERR,flag,msg);
    }

    @GetMapping("/select")
    @ApiOperation("模糊查询")
    public Result select(Integer index,Integer size,Rent rent,@RequestHeader("Authorization") String token){
        Result rentList = this.rentService.select(index,size,rent);
        return rentList;
    }

}
